//
//  WCBTest.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/4/26.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import WCDBSwift
import CooperSwift

class WCDBTestVC: UIViewController {
    
    private lazy var db : Database? = {
            //1.创建数据库
            let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            log.error(docPath)
            let database = Database(withPath: docPath + "/wcdb.db")
            return database
        }()
    
    override func viewDidLoad() {
        
        guard let db = db else { return }
        do {
            //创建数据库表
            try db.create(table:"sampleTable", of: Sample.self)
        } catch {
            print(error)
        }
        
    }
    
    @IBAction func insertTap(_ sender: Any) {
        guard let db = db else { return }
        let object = Sample()
        object.description = "sample_insert -->"
        object.content = "sample_insert --> content"
        object.title = "Title --> "
        do {
            try db.insert(objects: object, intoTable: "sampleTable")
        } catch {
            log.error(error)
        }
        log.info(object.lastInsertedRowID)
        
//        // Prepare data
//        for idx in 0...1000 {
//            let object = Sample()
//            object.identifier = idx
//            object.description = "sample_insert --> \(idx)"
//            // Insert
//            do {
//                try db.insert(objects: object, intoTable: "sampleTable")
//            } catch {
//                log.error(error)
//            }
//        }
    }
    
    @IBAction func readTap(_ sender: Any) {
        guard let db = db else { return }
        do {
            let objects: [Sample] = try db.getObjects(fromTable: "sampleTable")
            for obj in objects {
                log.info(obj)
                log.info("\(obj) --> identifier： \(String(describing: obj.identifier)) --> lastInsertedRowID： \(String(describing: obj.lastInsertedRowID))")
            }
            let objects1: [Sample] = try db.getObjects(fromTable: "sampleTable", where: Sample.Properties.identifier < 6, orderBy: [Sample.Properties.identifier])
            for obj in objects1 {
                log.info(obj)
                log.info("\(obj) --> \(String(describing: obj.identifier))")
            }
        } catch {
            log.error(error)
        }
    }
    
    @IBAction func updateTap(_ sender: Any) {
        guard let db = db else { return }
        let object = Sample()
        object.description = "sample_update"
        do {
            try db.update(table: "sampleTable", on: Sample.Properties.description, with: object, where: Sample.Properties.identifier > 0)
        } catch {
            log.error(error)
        }
    }
    
    @IBAction func deleteTap(_ sender: Any) {
        guard let db = db else { return }
        do {
            try db.delete(fromTable: "sampleTable")
        } catch {
            log.error(error)
        }
    }
    
}
